package com.bentley.kotlindemo.net.manager

import android.content.IntentFilter
import android.net.ConnectivityManager
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.bentley.kotlindemo.App

/**
 * <pre>
 *  ┌────────────────────────────────────────────
 *  │ @author  Lee  @date 2021/3/18 09:29
 *  ├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄
 *  │ @desc 监听网络状态变动
 *  └────────────────────────────────────────────
 * </pre>
 */
class NetworkStateManager private constructor() : DefaultLifecycleObserver {
    companion object {
        val instance: NetworkStateManager by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
            NetworkStateManager()
        }
    }

    private val networkStateReceive = NetworkStateReceive()
    override fun onResume(owner: LifecycleOwner) {
        super.onResume(owner)
        val filter = IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)
        App.INSTANCE.registerReceiver(networkStateReceive,filter)
    }

    override fun onPause(owner: LifecycleOwner) {
        super.onPause(owner)
        App.INSTANCE.unregisterReceiver(networkStateReceive)
    }
}